﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace WebScrapper.Helpers
{
    class AppUtil
    {
        public static String GetAuthenticatedPageContent(String url, String authenticationCookieData)
        {
            //string cookiedata = ".ASPXAUTH=FB8ADA49D4BFE4EF531A4539D0B74CCA6762F9CC6F62C8E...";

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            //set the user agent so it looks like IE to not raise suspicion 
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
            request.Method = "GET";
            //set the cookie in the request header
            request.Headers.Add("Cookie", authenticationCookieData);

            //get the response from the server
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string pageHtml = string.Empty;
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    pageHtml = reader.ReadToEnd();
                    //now we can scrape the contents of the secure page as needed
                    //since the page contents is now stored in our pagedata string
                }
            }
            response.Close();
            return pageHtml;
        }
    }
}
